//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.ComponentModel;
using System.Web.UI.WebControls;
using CommunityServer;
using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Files.Components;

namespace CommunityServer.Files.Controls
{

	public class RatingListing : FileGalleryTemplatedWebControl
	{
		CSContext csContext = CSContext.Current;

		#region Child Controls

		private IText AverageRating;
		private IText TotalVotes;
		private EntryRatePost RatePost;

		#endregion

		#region Public Properties

		[DefaultValue( null )]
		public virtual string ApplicationKey
		{
			get
			{
				Object state = ViewState["ApplicationKey"];
				if(state != null)
					return (string)state;
				return null;
			}
			set { ViewState["ApplicationKey"] = value; }
		}

		[DefaultValue( -1 )]
		public virtual int PictureID
		{
			get
			{
				Object state = ViewState["PictureID"];
				if(state != null)
					return (int)state;
				return -1;
			}
			set { ViewState["PictureID"] = value; }
		}

		private Entry entry = null;
		public Entry Entry
		{
			get { return entry; }
			set { entry = value; }
		}

		public bool HideOnNoVotes = true;

		#endregion

		#region Skin

		protected override void AttachChildControls()
		{
			AverageRating = FindText( "AverageRating" );
			TotalVotes = FindText( "TotalVotes" );
			RatePost = (EntryRatePost)FindControl( "RatePost" );
		}

		#endregion

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
			ApplicationKey = csContext.ApplicationKey;
			PictureID = csContext.PostID;
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			if (!CurrentFolder.EnableRatings)
			{
				this.Visible = false;
				return;
			}
		}

		private Folder _currentFolder;
		public override Folder CurrentFolder
		{
			get
			{
				if(_currentFolder == null)
					if (!Globals.IsNullorEmpty(this.ApplicationKey))
						_currentFolder = Folders.GetFolder(this.ApplicationKey);
					else if(!Globals.IsNullorEmpty(csContext.ApplicationKey))
						_currentFolder = Folders.GetFolder( csContext.ApplicationKey );
					else if (csContext.SectionID != -1)
						_currentFolder = Folders.GetFolder(csContext.SectionID);

				return _currentFolder;
			}
		}
		
		public override void DataBind()
		{
			base.DataBind ();
			BindRatings();
		}

		private void BindRatings()
		{
			// If we have no settings, return
			if((ApplicationKey == null) || (PictureID == -1))
				return;

			// Check if ratings were enabled
			if(!Folders.GetFolder(ApplicationKey).EnableRatings)
			{
				this.Visible = false;
				return;
			}

			// Get the picture if we don't have one
			if(entry == null)
				entry = Entries.GetEntry(CurrentFolder.SectionID, PictureID);

			// See if we should hide on no votes
			if(HideOnNoVotes && (entry.TotalRatings == 0))
			{
				Visible = false;
				return;
			}

			// Setup the Ratings
			if(TotalVotes != null)
				TotalVotes.Text = entry.TotalRatings.ToString();
			
			if(RatePost != null)
				RatePost.Post = entry;

			if(AverageRating != null)
				AverageRating.Text = entry.RatingAverage.ToString("0.0#");
		}
	}
}
